﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RMS.Models.Repository;

namespace RMS.Models.DatabaseManager
{
    public class UserManager : BaseManager
    {
        //UserContext db;
        EFDbContext db;
        User user; //User that the instance is checking

        public UserManager()
        {
            //db = new UserContext();
            db = new EFDbContext();
        }

        public IEnumerable<User> AllUsers
        {
            get
            {
                return db.Users;
            }
        }

        public User GetUser(int userID)
        {
            return db.Users.Find(userID);
        }

        public bool ValidateEmail(string userEmail)
        {
            
            IQueryable<User> query = db.Users;
            User userToReturn = null;

            query = query.Where(u => u.Us_Email == userEmail);

            foreach (User u in query)
                userToReturn = u;

            if (userToReturn != null)
            {
                user = userToReturn;
                return true;
            }
            else // changed: originally was set to return false
                return false;
           
            
        }

        public bool AddNewUser(string fName, string lname, string mInt, string address, string city, string state, string zip, string phone, string email, string pass)
        {
            User newUser = new User();
            newUser.Us_FName = fName;
            newUser.Us_LName = lname;
            newUser.Us_MInt = mInt;
            newUser.Us_Address = address;
            newUser.Us_City = city;
            newUser.Us_State = state;
            newUser.Us_Zip = zip;
            newUser.Us_Phone = phone;
            newUser.Us_Password = pass;
            newUser.Us_Email = email;
            //newUser.Us_IsAdmin = isAdmin;

            user = newUser;
            db.Users.Add(newUser);
            db.SaveChanges();

            return true;
        }

        public void UpdateUser(User updatedUser)
        {
            user = updatedUser;
            db.SaveChanges();
        }

        public void DeleteUser(User userToDelete)
        {
            db.Users.Remove(userToDelete);
            db.SaveChanges();
        }

        public bool ValidatePassword(string password)
        {
            return (user.Us_Password.Equals(password));
        }

        public void ChangePassword(string newPassword)
        {
            user.Us_Password = newPassword;
            db.Entry(user).GetDatabaseValues().SetValues(user);
            db.SaveChanges();
        }

        public int GetUserID()
        {
            return user.Us_ID;
        }

        public string GetUserEmail()
        {
            return user.Us_Email;
        }

        public string GetUserPassword()
        {
            return user.Us_Password;
        }

        public string GetUserFName()
        {
            return user.Us_FName;
        }

        public bool GetIsAdmin()
        {
            return user.Us_IsAdmin;
        }
    }
}